<?php

namespace common\model;

use think\model\RelationModel;

class UserModel extends RelationModel
{
    const IS_SYSTEM_NO = 'no';
    const IS_SYSTEM_YES = 'yes';
    const STATUS_ENABLE = 0;
    const STATUS_DISABLE = 1;
    const ACTIVE_DISABLE = 0;  // 用户激活状态-未启用
    const ACTIVE_ENABLE = 1; // 用户激活状态-启用

    //自动验证
    protected $_validate = [
        ['name', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['name', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['phone', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['phone', '1,20', '', self::EXISTS_VALIDATE, 'length'],
        ['phone', '/^(\+?86-?)?1(3|4|5|6|7|8|9)[0-9]{9}$/', 'Please enter the correct phone number', self::EXISTS_VALIDATE, 'reg'],
        ['phone', '', '', self::EXISTS_VALIDATE, 'unique'],
        ['email', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['email', '', '', self::EXISTS_VALIDATE, 'email'],
        ['status', [1, 0], '', self::EXISTS_VALIDATE, 'in'],
        ['sex', ['male', 'female'], '', self::EXISTS_VALIDATE, 'in'],
        ['password', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['password', '', '', self::EXISTS_VALIDATE, 'password_strength'],
        ['scope', '0,255', '', self::EXISTS_VALIDATE, 'length'],
        ['unionid', '0,30', '', self::EXISTS_VALIDATE, 'length'],
        ['active', [1, 0], '', self::EXISTS_VALIDATE, 'in']
    ];

    // 自动完成
    protected $_auto = [
        ['password', 'create_pass', self::MODEL_BOTH, 'function'],
        ['password', '', self::MODEL_BOTH, 'ignore'],
        ['created', 'time', self::MODEL_INSERT, 'function'],
        ['uuid', 'create_uuid', self::MODEL_INSERT, 'function']
    ];

    // 隐藏字段
    protected $hidden = ['password'];

    /**
     * 获取器：最后上线时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getLastOnlineTimeAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }

    /**
     * 获取器：创建时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getCreatedAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }
}
